package client;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;

public class ChatClient extends JFrame implements KeyListener, ActionListener, FocusListener {
    public static final String appName = "Chat Tool";
    public static final String serverText = "127.0.0.1";
    public static final String portText = "3500";
    public static final String nickText = "YourName";
    public static ChatClient client;
    public static ChatClient log;
    public static ChatClient regis;
    public static ChatClient inf;
    public static ChatClient fri;
    public static ChatClient info;
    public static SQL LogIn;
    JPanel northPanel, southPanel, centerPanel, eastPanel, logCenterP, logSouthP, infCenterP, infSouthP;
    static JList UserList, friList;
    JTextField txtHost, txtPort, msgWindow, txtNick, username, rusername, infnamw, infpwdw, infsexw, infnumw, infusrw;
    JPasswordField password, rpassword, rconfpassword;
    JButton buttonConnect, buttonSend, btnfresh, buttonFile, btnlog, btnreg, reg, infw, infr, buttonReturn;
    JLabel lb1, lb2, usr, pwd, confpwd, infnam, infpwd, infsex, infnum, infusr;
    JLabel infnam1, infpwd1, infsex1, infnum1, infusr1, infsta1;
    JLabel infnam2, infpwd2, infsex2, infnum2, infusr2, infsta2;
    JScrollPane sc, sc1;
    static ClientKernel ck;
    ClientHistory historyWindow, historyWindow1;
    JFileChooser jfc; //鏂囦欢閫夋嫨鍣紝璁剧疆鍒濆璺緞涓�"F:\\"
    private String lastMsg = "";
    public static String usrin = "";
    public static String pwdin = "";
    public static String rusr = "";
    public static String rpwdin1 = "";
    public static String rpwdin2 = "";
    public static String value = "";
    public static int v;

    /**
     * Creates a new instance of Class
     */
    public ChatClient() {
        uiInit();
        txtHost.setText("127.0.0.1");
        txtPort.setText("3500");
    }

    public ChatClient(int i) {
        logInit();
    }

    public ChatClient(int i, int n) {
        regInit();
    }

    public ChatClient(int i, int j, int k) {
        InfoInit();
    }

    public ChatClient(int i, int j, int k, int m, String infosel) {
        InfoInit1(infosel);
    }

    public ChatClient(String[] frilist) {
        friListInit(frilist);
    }

    public void friListInit(String[] frilist) {
        JPanel infCenterP = new JPanel();
        JPanel infSouthP = new JPanel(new GridLayout(1, 2));
        JButton addfri = new JButton("填加好友");
        addfri.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                JFrame addf = new JFrame("添加好友");
                JPanel addp = new JPanel(new GridLayout(3, 1));
                JTextField txt = new JTextField();
                JLabel lb = new JLabel("请输入对方id：");
                JButton add = new JButton("发送");
                add.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        int i = LogIn.isfri(usrin, txt.getText());
                        if (i == 0)
                            System.out.println("该用户不存在");
                        else if (i == 1)
                            System.out.println("你们已经是好友了");
                        else if (i == 2) {
                            System.out.println("请求已发送");
                            addf.setVisible(false);
                            //添加发送函数
                            try {
                                ck.addFriend(txt.getText());
                            } catch (IOException ioException) {
                                ioException.printStackTrace();
                            }
                        }
                    }
                });
                addp.add(lb, JLabel.CENTER);
                addp.add(txt);
                addp.add(add);
                addf.add(addp);
                addf.setSize(200, 100);
                addf.setLocation(200, 200);
                addf.setVisible(true);


            }
        });
        friList = new JList(frilist);
        friList.setPreferredSize(new Dimension(100, 500));
        JScrollPane scrollPane = new JScrollPane(friList);
        infr = new JButton("查看信息");
        infr.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (friList.getSelectedIndex() != -1) {
                    String infosel = (String) friList.getSelectedValue();
                    info = new ChatClient(1, 1, 1, 1, infosel);
                    info.setTitle("信息查看");
                    info.setSize(300, 350);
                    info.setLocation(100, 100);
                    info.setVisible(true);
                    info.setResizable(false);
                } else {
                    System.out.println("未选择");
                }

            }
        });
        infSouthP.add(addfri);
        infSouthP.add(infr);
        this.add(scrollPane, BorderLayout.CENTER);
        this.add(infSouthP, BorderLayout.SOUTH);
    }

    public void logInit() {
        logCenterP = new JPanel(new GridLayout(2, 2));
        logSouthP = new JPanel(new GridLayout(1, 2));
        usr = new JLabel("用户名", JLabel.CENTER);
        pwd = new JLabel("密码", JLabel.CENTER);
        username = new JTextField();
        password = new JPasswordField();
        btnlog = new JButton("登录");
        btnreg = new JButton("注册");
        btnlog.addActionListener(this);
        username.addKeyListener(this);
        btnreg.addActionListener(this);
        logCenterP.add(usr);
        logCenterP.add(username);
        logCenterP.add(pwd);
        logCenterP.add(password);
        logSouthP.add(btnlog);
        logSouthP.add(btnreg);
        this.add(logCenterP, BorderLayout.NORTH);
        this.add(logSouthP, BorderLayout.SOUTH);
    }

    public void regInit() {
        logCenterP = new JPanel(new GridLayout(3, 2));
        logSouthP = new JPanel(new GridLayout(1, 2));
        usr = new JLabel("用户名", JLabel.CENTER);
        pwd = new JLabel("密码", JLabel.CENTER);
        confpwd = new JLabel("确认密码", JLabel.CENTER);
        rusername = new JTextField();
        rpassword = new JPasswordField();
        rconfpassword = new JPasswordField();
        reg = new JButton("注册");
        reg.addActionListener(this);
        JButton back = new JButton("返回");
        back.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                regis.setVisible(false);
                log.setVisible(true);
            }
        });
        logSouthP.add(back);
        logCenterP.add(usr);
        logCenterP.add(rusername);
        logCenterP.add(pwd);
        logCenterP.add(rpassword);
        logCenterP.add(confpwd);
        logCenterP.add(rconfpassword);
        logSouthP.add(reg);
        this.add(logCenterP, BorderLayout.NORTH);
        this.add(logSouthP, BorderLayout.SOUTH);
    }

    public void InfoInit() {
        JPanel north = new JPanel();
        JLabel lb = new JLabel("个人信息");
        GridLayout layout = new GridLayout(5, 2);
        infCenterP = new JPanel(layout);
        infSouthP = new JPanel(new GridLayout(1, 1));
        layout.setHgap(10);
        layout.setVgap(10);
        infusr = new JLabel("用户名", JLabel.CENTER);
        infusrw = new JTextField();
        infusrw.setEditable(false);
        infpwd = new JLabel("密码", JLabel.CENTER);
        infpwdw = new JTextField();
        infnam = new JLabel("姓名", JLabel.CENTER);
        infnamw = new JTextField();
        infsex = new JLabel("性别", JLabel.CENTER);
        infsexw = new JTextField();
        infnum = new JLabel("手机号码", JLabel.CENTER);
        infnumw = new JTextField();
        infw = new JButton("修改");
        infusrw.setText(value);
        infpwdw.setText(LogIn.getpwd(value));
        infnamw.setText(LogIn.getnam(value));
        infsexw.setText(LogIn.getsex(value));
        infnumw.setText(LogIn.getnum(value));
        infw.addActionListener(this);
        System.out.println(value);
        System.out.println(usrin);
        System.out.println(UserList.getSelectedIndex());
        if (value.equals(usrin)) {
            infpwdw.setEditable(true);
            infnamw.setEditable(true);
            infsexw.setEditable(true);
            infnumw.setEditable(true);
        } else {
            lb.setText("查看资料");
            infpwdw.setEditable(false);
            infnamw.setEditable(false);
            infsexw.setEditable(false);
            infnumw.setEditable(false);
            infw.setVisible(false);
        }
        infCenterP.add(infusr);
        infCenterP.add(infusrw);
        infCenterP.add(infpwd);
        infCenterP.add(infpwdw);
        infCenterP.add(infnam);
        infCenterP.add(infnamw);
        infCenterP.add(infsex);
        infCenterP.add(infsexw);
        infCenterP.add(infnum);
        infCenterP.add(infnumw);
        infSouthP.add(infw);
        north.add(lb);
        this.add(north, BorderLayout.NORTH);
        this.add(infCenterP, BorderLayout.CENTER);
        this.add(infSouthP, BorderLayout.SOUTH);
    }

    public void InfoInit1(String infosel) {
        JPanel north = new JPanel();
        JLabel lb = new JLabel("查看资料");
        GridLayout layout = new GridLayout(6, 2);
        infCenterP = new JPanel(layout);
        layout.setHgap(10);
        layout.setVgap(10);
        infusr1 = new JLabel("用户名", JLabel.CENTER);
        infusr2 = new JLabel("", JLabel.CENTER);
        infpwd1 = new JLabel("密码", JLabel.CENTER);
        infpwd2 = new JLabel("", JLabel.CENTER);
        infnam1 = new JLabel("姓名", JLabel.CENTER);
        infnam2 = new JLabel("", JLabel.CENTER);
        infsex1 = new JLabel("性别", JLabel.CENTER);
        infsex2 = new JLabel("", JLabel.CENTER);
        infnum1 = new JLabel("手机号码", JLabel.CENTER);
        infnum2 = new JLabel("", JLabel.CENTER);
        infsta1 = new JLabel("状态", JLabel.CENTER);
        infsta2 = new JLabel("", JLabel.CENTER);
        infusr2.setText(infosel);
        infpwd2.setText(LogIn.getpwd(infosel));
        infnam2.setText(LogIn.getnam(infosel));
        infsex2.setText(LogIn.getsex(infosel));
        infnum2.setText(LogIn.getnum(infosel));
        if (LogIn.getsta(infosel).equals("0"))
            infsta2.setText("离线");
        else if (LogIn.getsta(infosel).equals("1"))
            infsta2.setText("在线");
        infCenterP.add(infusr1);
        infCenterP.add(infusr2);
        infCenterP.add(infpwd1);
        infCenterP.add(infpwd2);
        infCenterP.add(infnam1);
        infCenterP.add(infnam2);
        infCenterP.add(infsex1);
        infCenterP.add(infsex2);
        infCenterP.add(infnum1);
        infCenterP.add(infnum2);
        infCenterP.add(infsta1);
        infCenterP.add(infsta2);
        north.add(lb);
        this.add(north, BorderLayout.NORTH);
        this.add(infCenterP, BorderLayout.CENTER);
    }

    public void uiInit() {
        setLayout(new BorderLayout());
        //鍒涘缓North
        northPanel = new JPanel(new GridLayout(0, 2));
        centerPanel = new JPanel(null);
        lb1 = new JLabel("聊天室");
        lb2 = new JLabel("私聊窗口");
        northPanel.add(new JLabel("Host address:"));
        northPanel.add(txtHost = new JTextField(ChatClient.serverText));
        northPanel.add(new JLabel("Port:"));
        northPanel.add(txtPort = new JTextField(ChatClient.portText));
        northPanel.add(new JLabel("Nick:"));
        northPanel.add(txtNick = new JTextField(ChatClient.nickText));
        northPanel.add(buttonReturn = new JButton("退出私聊"));
        buttonReturn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                ck.privateMsg = false;
            }
        });
        northPanel.add(buttonConnect = new JButton("好友列表"));
        buttonConnect.addActionListener(this);
        txtHost.addKeyListener(this);
        txtHost.addFocusListener(this);
        txtNick.addFocusListener(this);
        txtNick.addKeyListener(this);
        txtNick.setEditable(false);
        txtPort.addKeyListener(this);
        txtPort.addFocusListener(this);
        buttonConnect.addKeyListener(this);
        this.add(northPanel, BorderLayout.NORTH);
        //鍒涘缓Sourth
        southPanel = new JPanel();
        southPanel.add(msgWindow = new JTextField(20));
        southPanel.add(buttonSend = new JButton("Send"));
        buttonSend.addActionListener(this);
        msgWindow.addKeyListener(this);

        buttonFile = new JButton("选择文件");  //鎸夐挳锛屽崟鍑诲搷搴斾簨浠讹紝鎵撳紑鏂囦欢閫夋嫨鍣�
        jfc = new JFileChooser("D:\\"); //鏂囦欢閫夋嫨鍣紝璁剧疆鍒濆璺緞涓�"F:\\"

        class myactionlistener implements ActionListener  //创建动作监听者者
        {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    int a = jfc.showOpenDialog(null);  //保存文件，指定路径
                    if (a == jfc.APPROVE_OPTION) {
                        System.out.println("用户选择了文件：" + jfc.getSelectedFile().getName());
                        File file = jfc.getSelectedFile();
                        ck.sendFile(file);
                    }
                } catch (Exception k) {
                    System.out.println("载入图片失败");
                }
            }
        }
//娣诲姞
        buttonFile.addActionListener(new myactionlistener());
        southPanel.add(buttonFile);   //闈㈡澘娣诲姞鎸夐挳

        this.add(southPanel, BorderLayout.SOUTH);
        //鍒涘缓Center
        historyWindow = new ClientHistory();
        historyWindow1 = new ClientHistory();
        sc = new JScrollPane(historyWindow);
        sc.setAutoscrolls(true);
        sc1 = new JScrollPane(historyWindow1);
        sc1.setAutoscrolls(true);
        centerPanel.setOpaque(true);
        lb1.setOpaque(true);
        lb2.setOpaque(true);
        sc.setOpaque(true);
        sc1.setOpaque(true);
        lb1.setLocation(5, 0);
        lb1.setSize(200, 20);
        sc.setSize(350, 125);
        sc.setLocation(5, 20);
        lb2.setLocation(5, 150);
        lb2.setSize(200, 20);
        sc1.setSize(350, 125);
        sc1.setLocation(5, 170);
        centerPanel.add(lb1);
        centerPanel.add(sc);
        centerPanel.add(lb2);
        centerPanel.add(sc1);
        this.add(centerPanel, BorderLayout.CENTER);
        //鍒涘缓East
        String[] str = {};
        eastPanel = new JPanel(new BorderLayout());
        UserList = new JList(str);
        btnfresh = new JButton("信息");
        btnfresh.setPreferredSize(new Dimension(0, 30));
        eastPanel.add(UserList, BorderLayout.CENTER);
        eastPanel.add(btnfresh, BorderLayout.SOUTH);
        eastPanel.setPreferredSize(new Dimension(80, 80));
        UserList.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (UserList.getSelectedIndex() != -1) {
                    if (e.getClickCount() == 2) {
                        ck.privateNick = (String) UserList.getSelectedValue();
                        ck.privateMsg = true;
                    }
                }
            }
        });
        UserList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                if (UserList.getSelectedIndex() != -1) {
                    value = (String) UserList.getSelectedValue();
                    System.out.println((String) UserList.getSelectedValue());
                }
            }
        });
        btnfresh.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
//              inf = new ChatClient(1,1,1);
//              inf.setSize(300, 350);
//              inf.setLocation(100, 100);
//              inf.setVisible(true);
//              inf.setResizable(false);
                if (ck != null) {
                    try {
                        ck.broadCastUsers();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }

                if (UserList.getSelectedIndex() != -1) {
                    inf = new ChatClient(1, 1, 1);
                    inf.setTitle("信息查看");
                    inf.setSize(300, 350);
                    inf.setLocation(100, 100);
                    inf.setVisible(true);
                    inf.setResizable(false);
                } else {
                    System.out.println("信息已刷新");
                }

            }
        });
        this.add(eastPanel, BorderLayout.EAST);
    }

    public static void main(String args[]) {
        File file2 = new File("pri.html");
        if(file2.exists())
            while (file2.delete()) ;
        File file1 = new File("pub.html");
        if(file1.exists())
            while (file1.delete()) ;

        client = new ChatClient();
        client.setTitle(client.appName);
        client.setSize(450, 500);
        client.setLocation(100, 100);
        client.setResizable(false);
        client.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        client.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.out.println("已下线");
                LogIn.Offline(usrin);
                System.exit(0);
            }
        });
        client.setVisible(false);
        client.msgWindow.requestFocus();
        log = new ChatClient(1);
        log.setTitle("登录");
        log.setSize(350, 110);
        log.setLocation(100, 100);
        log.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        log.setVisible(true);
        log.setResizable(false);
        regis = new ChatClient(1, 1);
        regis.setTitle("注册");
        regis.setSize(350, 130);
        regis.setLocation(100, 100);
        regis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        regis.setVisible(false);
        regis.setResizable(false);
//        inf = new ChatClient(1,1,1);
//        inf.setSize(300, 350);
//        inf.setLocation(100, 100);
//        inf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        inf.setVisible(true);
//        inf.setResizable(false);
        LogIn = new SQL();
        System.out.println();


    }

    public void receiveFriend(String name) {
        //you write
        JFrame addf = new JFrame("好友请求");
        JPanel addp = new JPanel();
        JPanel addp1 = new JPanel();
        JLabel lb = new JLabel(name + "请求添加您为好友");
        JButton add = new JButton("接收");
        JButton deny = new JButton("拒绝");
        add.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                LogIn.addfri(usrin, name);
                LogIn.addfri(name, usrin);
                addf.setVisible(false);
                //同意添加好友
                try {
                    ck.acFriend(name,"yes");
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
        deny.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                addf.setVisible(false);
                try {
                    ck.acFriend(name,"no");
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
        addp.add(lb, JLabel.CENTER);
        addp1.add(deny);
        addp1.add(add);
        addf.add(addp, BorderLayout.CENTER);
        addf.add(addp1, BorderLayout.SOUTH);
        addf.setSize(200, 100);
        addf.setLocation(200, 200);
        addf.setVisible(true);
    }

    //淇敼鍦ㄧ嚎鐢ㄦ埛
    public void ChangeUsers(String[] temp) {
        DefaultListModel dlm = new DefaultListModel();
        if (temp.length < 8)
            for (int i = 0; i < temp.length; i++) {
                dlm.addElement(temp[i]);
            }
        else {
            for (int i = 0; i < 8; i++) {
                dlm.addElement(temp[i]);
            }
        }
        UserList.setModel(dlm);
    }

    public void addMsg(String str) throws IOException {
        historyWindow.addText(str);
        String saveFile = "pub.html";
        File file = new File(saveFile);
        adms(str, file);
        String path = "file:" + file.getAbsolutePath();
        historyWindow.setEditable(false);
        try {
            historyWindow.setPage(path);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void addPrivateMsg(String str) throws IOException {
        historyWindow1.addText(str);
        String saveFile = "pri.html";
        File file = new File(saveFile);
        adms(str, file);
        String path = "file:" + file.getAbsolutePath();
        historyWindow1.setEditable(false);
        try {
            historyWindow1.setPage(path);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void adms(String str, File file) {

        FileOutputStream fos = null;
        OutputStreamWriter osw = null;

        try {
            if (!file.exists()) {
                boolean hasFile = file.createNewFile();
                if (hasFile) {
                    System.out.println("file not exists, create new file");
                }
                fos = new FileOutputStream(file);
            } else {
                System.out.println("file exists");
                fos = new FileOutputStream(file, true);
            }

            osw = new OutputStreamWriter(fos, "utf-8");
            osw.write(str); //写入内容
            osw.write("\r\n");  //换行
        } catch (Exception e) {
            e.printStackTrace();
        } finally {   //关闭流
            try {
                if (osw != null) {
                    osw.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void connect() {
        try {
            if (ck != null) {
                //ck.dropMe();
                System.out.println("You have been online!");
            } else {
                ck = new ClientKernel(txtHost.getText(), Integer.parseInt(txtPort.getText()));
                ck.setNick(txtNick.getText());
                if (ck.isConnected()) {
                    ck.addClient(this);
                    addMsg("<font color=\"#00ff00\">connected! Local Port:" + ck.getLocalPort() + "</font>");
                } else {
                    addMsg("<font color=\"#ff0000\">connect failed!</font>");
                }
                ck.broadCastUsers();
                ck.imOnline();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void send() throws IOException {
        String toSend = msgWindow.getText();
        ck.sendMessage(toSend);
        lastMsg = "" + toSend;
        msgWindow.setText("");
    }

    public void keyPressed(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
        if (e.getSource() == msgWindow && e.getKeyCode() == KeyEvent.VK_UP) msgWindow.setText(lastMsg);
    }

    public void keyTyped(KeyEvent e) {
        if (e.getKeyChar() == KeyEvent.VK_ENTER) {
            if (e.getSource() == msgWindow) {
                try {
                    send();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
            if (e.getSource() == txtNick) {
                connect();
                msgWindow.requestFocus();
            }
            if (e.getSource() == txtHost) txtPort.requestFocus();
            if (e.getSource() == txtPort) txtNick.requestFocus();
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonConnect) {
            String[] list = new String[100];
            LogIn.getfri(usrin, list);
            fri = new ChatClient(list);
            fri.setTitle("好友列表");
            fri.setSize(200, 500);
            fri.setLocation(100, 100);
            fri.setResizable(true);
            ;
            fri.setVisible(true);
        }
        if (e.getSource() == buttonSend) {
            try {
                send();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
        if (e.getSource() == btnlog) {
            usrin = username.getText();
            pwdin = new String(password.getPassword());
            if (LogIn.IsTrue(usrin, pwdin) == 1) {
                int i = LogIn.Online(usrin);
                if (i == 1) {
                    log.setVisible(false);
                    client.txtNick.setText(usrin);
                    client.connect();
                    client.setVisible(true);
                    System.out.println("登录成功");
                }
            } else {
                System.out.println("用户名或密码错误");
            }
        }
        if (e.getSource() == btnreg) {
            log.setVisible(false);
            regis.setVisible(true);
        }
        if (e.getSource() == reg) {
            rusr = regis.rusername.getText();
            rpwdin1 = new String(regis.rpassword.getPassword());
            rpwdin2 = new String(regis.rconfpassword.getPassword());
            if (!(rpwdin1.equals(rpwdin2))) {
                System.out.println("两次密码不一致");
            } else {
                if (LogIn.Insert(rusr, rpwdin1) == 1) {
                    System.out.println("注册成功");
                    log.setVisible(true);
                    regis.setVisible(false);
                } else {
                    System.out.println("用户名已存在");
                }
            }
        }
        if (e.getSource() == infw) {
            int i = 0;
            i = LogIn.setpwd(usrin, infpwdw.getText());
            i = LogIn.setnam(usrin, infnamw.getText());
            i = LogIn.setsex(usrin, infsexw.getText());
            i = LogIn.setnum(usrin, infnumw.getText());
            if (i == 1)
                System.out.println("信息修改完成");
        }
    }

    public void focusGained(FocusEvent e) {
        if (e.getSource() == txtHost && txtHost.getText().equals(ChatClient.serverText)) txtHost.setText("");
        if (e.getSource() == txtPort && txtPort.getText().equals(ChatClient.portText)) txtPort.setText("");
        //if (e.getSource() == txtNick && txtNick.getText().equals(ChatClient.nickText)) txtNick.setText("");
    }

    public void focusLost(FocusEvent e) {
        if (e.getSource() == txtPort && txtPort.getText().equals("")) txtPort.setText(ChatClient.portText);
        if (e.getSource() == txtHost && txtHost.getText().equals("")) txtHost.setText(ChatClient.serverText);
        if (e.getSource() == txtNick && txtNick.getText().equals(ChatClient.nickText))
            txtNick.setText(ChatClient.nickText);
    }

    class ClientHistory extends JEditorPane {
        public ClientHistory() {
            super("text/html", "");
            setEditable(false);
            setAutoscrolls(true);
        }

        public void addText(String str) {
            String html = getText();
            int end = html.lastIndexOf("</body>");
            String startStr = html.substring(0, end);
            String endStr = html.substring(end, html.length());
            String newHtml = startStr + "<br>" + str + endStr;
            setText(newHtml);
            setSelectionStart(newHtml.length() - 1);
            setSelectionEnd(newHtml.length());
        }

        public void clear() {
            setText("");
        }
    }

    public class MyFilter extends FileFilter {
        private String[] filterString = null;

        public MyFilter(String[] filStrings) {
            this.filterString = filStrings;
        }

        public boolean accept(File file) {
            if (file.isDirectory()) return false;
            for (int i = 0; i < filterString.length; ++i)
                if (file.getName().endsWith(filterString[i]))
                    return true;
            return false;
        }

        public String getDescription() {
            String ss = "";
            for (int i = 0; i < filterString.length; ++i)
                ss += " *" + filterString[i];
            return ("Txt Files(" + ss + ")");                  //杩斿洖鏄剧ず鏂囦欢绫诲瀷鐨勬弿杩�
        }
    }
}

